package com.example.ktpspringboot.controller;

import com.example.ktpspringboot.entity.Course;
import com.example.ktpspringboot.entity.Student;
import com.example.ktpspringboot.entity.Teacher;
import com.example.ktpspringboot.service.impl.CourseServiceImpl;
import com.example.ktpspringboot.utils.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CourseController {

    @Autowired
    private CourseServiceImpl courseServiceImpl;

    @Autowired
    private SnowflakeUtil snowflakeUtil;

    @RequestMapping("/getStudyCourse")
    public List<Course> getStudyCourse(@RequestBody Map<String, Object> json) {
        String username = (String) json.get("username");
        String role = (String) json.get("role");
        return courseServiceImpl.getStudyCourse(username, role);
    }

    @RequestMapping("/getTeachCourse")
    public List<Course> getTeachCourse(@RequestBody Map<String, Object> json) {
        String username = (String) json.get("username");
        String role = (String) json.get("role");
        return courseServiceImpl.getTeachCourse(username, role);
    }

    @RequestMapping("/getHelpCourse")
    public List<Course> getHelpCourse(@RequestBody Map<String, Object> json) {
        String username = (String) json.get("username");
        String role = (String) json.get("role");
        return courseServiceImpl.getHelpCourse(username, role);
    }

    @RequestMapping("/joinCourse")
    public String joinCourse(@RequestBody Map<String, Object> json) {
        String username = (String) json.get("username");
        String role = (String) json.get("role");
        String code = (String) json.get("code");
        System.out.println("加入的课程" + json);
        return courseServiceImpl.joinCourse(code, username, role);
    }

    @RequestMapping("/getTopCourse")
    public List<Course> getTopCourse(@RequestBody Map<String, Object> json) {
        String username = (String) json.get("username");
        String role = (String) json.get("role");
        return courseServiceImpl.getTopCourse(username, role);
    }

    @RequestMapping("/establishCourse")
    public String establishCourse(@RequestBody Map<String, Object> json) {
        Course course = new Course();
        String username = (String) json.get("username");
        String courseName = (String) json.get("courseName");
        String studyClass = (String) json.get("studyClass");
        String courseTime = (String) json.get("courseTime");
        String studyTime = (String) json.get("studyTime");
        String teachingMode = (String) json.get("teachingMode");
        String courseIntroduce = (String) json.get("courseIntroduce");
        String courseSpace = (String) json.get("courseSpace");
        courseServiceImpl.dealWithTeachingMode(teachingMode, course);
        course.setCourseTeacher(courseServiceImpl.establishCourseQueryTeaName(username));
        course.setUuid(String.valueOf(snowflakeUtil.generateId()));
        course.setCourseName(courseName);
        course.setStudyClass(studyClass);
        course.setCourseTime(courseTime);
        course.setStudyTime(studyTime);
        course.setCourseIntroduce(courseIntroduce);
        course.setCourseSpace(courseSpace);
        course.setCourseCode(courseServiceImpl.generateEnrollmentCode());
        course.setCourseNumber(courseServiceImpl.gainStudentNumber(studyClass));
        course.setBackgroundImage(courseServiceImpl.preserveUrl());
        return courseServiceImpl.establishCourse(course, username);
//        System.out.println(course);
//        System.out.println("role:" + role + " || username:" + username + " || courseName:" + courseName + " || studyClass:" + studyClass +
//                " || courseTime:" + courseTime + " || studyTime:" + studyTime + " || teachMode:" + teachingMode + " || courseIntroduce:" +
//                courseIntroduce + " || teachSpace:" + teachingSpace);
    }

    @RequestMapping("/topping")
    public String topping(@RequestBody Course course) {
        return courseServiceImpl.topping(course.getCourseCode());
    }

    @RequestMapping("/cancelTopping")
    public String cancelTopping(@RequestBody Course course) {
        return courseServiceImpl.cancelTopping(course.getCourseCode());
    }


    /**
     * 通过课程号查询到课程id，再通过课程id查询到教师id，再通过教师id查询到教师姓名和学校
     * @param courseCode
     * @return Map<String, Object>,返回教室列表以及教师数量
     */
    @RequestMapping("/getCourseIdByCode")
    public Map<String, Object> getCourseIdByCode(@RequestParam String courseCode) {
        List<Teacher> teacher = courseServiceImpl.getCourseIdByCode(courseCode);
        int teacherNums = courseServiceImpl.sumTeacher(courseCode);
        Map<String, Object> result = new HashMap<>();
        result.put("teachers", teacher);
        result.put("teacherNums", teacherNums);
        return result;
    }

    /**
     * 通过课程号查询到课程id，再通过课程id查询到学生id，再通过学生id查询到学生学号、姓名和学校
     * @param courseCode
     * @return Map<String, Object>,返回学生列表以及学生数量
     */
    @RequestMapping("/getCourseIdByCodeForStudent")
    public Map<String, Object> getCourseIdByCodeForStudent(@RequestParam String courseCode) {
        List<Student> student = courseServiceImpl.getCourseIdByCodeForStudent(courseCode);
        int studentsNums = courseServiceImpl.sumStudent(courseCode);
        Map<String, Object> result = new HashMap<>();
        result.put("students", student);
        result.put("studentsNums", studentsNums);
        return result;
    }

}
